package com.yjzx.util.common.util.map;

/**
 * @author yjzx
 * @date 2024/8/27
 * @description Node
 */
public class Node<T> {
    private final long key;
    private final T value;
    private final long clock;

    Node(long clock, T value) {
        this.clock = clock;
        this.key = clock < 0 ? Long.MAX_VALUE : System.currentTimeMillis() + (clock * 1000);
        this.value = value;
    }

    /**
     * key 是起始时间戳
     *
     * @return
     */
    public long getKey() {
        return key;
    }

    /**
     * 如果超过时间戳，则返回null
     *
     * @return
     */
    public T getValue() {
        return System.currentTimeMillis() > key ? null : value;

    }

    public long getClock() {
        return clock;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Node<?> node = (Node<?>) o;

        if (clock != node.clock) {
            return false;
        }
        return value != null ? value.equals(node.value) : node.value == null;
    }

    @Override
    public int hashCode() {
        int result = value != null ? value.hashCode() : 0;
        result = 31 * result + (int) (clock ^ (clock >>> 32));
        return result;
    }
}
